define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, undefined, Echarts, undefined) {


    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wu/hjdevicelog/index',
                    add_url: 'wu/hjdevicelog/add',
                    edit_url: 'wu/hjdevicelog/edit',
                    del_url: 'wu/hjdevicelog/del',
                    multi_url: 'wu/hjdevicelog/multi',
                    table: 'wu_hjdevicelog',
                }
            });

            var table = $("#table");

            var commandtypelist = {"FE02":__('A油箱溢位'),"FE03":__('B油箱溢位'),"FE04":__('A油箱A-1抽油馬達'),"FE05":__('A油箱A-2抽油馬達'),
                "FE06":__('B油箱B-1抽油馬達'),"FE07":__('B油箱B-2抽油馬達'),"FE08":__('抽油馬達異常告警'),"FE09":__('監控裝置異常告警')};
            if (Config.isWsddevice) {
                commandtypelist = {"FE00":__('溫度'),"FE01":__('濕度')};
            }
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        //{field: 'hjdevice_id', title: __('Hjdevice_id')},
                        {field: 'commandtype', title: __('類型'), searchList: commandtypelist, formatter: Table.api.formatter.normal},
                        {field: 'value1', title: __('數值'), operate:false},
                        {field: 'value2', title: __('Value2'), operate:false, visible:!Config.isWsddevice},
                        {field: 'value3', title: __('Value3'), operate:false, visible:!Config.isWsddevice},
                        //{field: 'value4', title: __('Value4'), operate:false},
                        {field: 'commandinfo', title: __('Commandinfo'), operate:false},
                        {field: 'create_time', title: __('時間'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'hjdevice.id', title: __('Hjdevice.id')},
                        //{field: 'hjdevice.client_id', title: __('Hjdevice.client_id')},
                        //{field: 'hjdevice.deviceid', title: __('Hjdevice.deviceid')},
                        //{field: 'hjdevice.name', title: __('Hjdevice.name')},
                        //{field: 'hjdevice.wd_newvalue', title: __('Hjdevice.wd_newvalue'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sd_newvalue', title: __('Hjdevice.sd_newvalue'), operate:'BETWEEN'},
                        //{field: 'hjdevice.yc_a_newvalue', title: __('Hjdevice.yc_a_newvalue'), operate:'BETWEEN'},
                        //{field: 'hjdevice.yc_b_newvalue', title: __('Hjdevice.yc_b_newvalue'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_a_newvalue1', title: __('Hjdevice.sxmda_a_newvalue1'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_a_newvalue2', title: __('Hjdevice.sxmda_a_newvalue2'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_a_newvalue3', title: __('Hjdevice.sxmda_a_newvalue3'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_a_newvalue4', title: __('Hjdevice.sxmda_a_newvalue4'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_b_newvalue1', title: __('Hjdevice.sxmda_b_newvalue1'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_b_newvalue2', title: __('Hjdevice.sxmda_b_newvalue2'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_b_newvalue3', title: __('Hjdevice.sxmda_b_newvalue3'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_b_newvalue4', title: __('Hjdevice.sxmda_b_newvalue4'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_c_newvalue1', title: __('Hjdevice.sxmda_c_newvalue1'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_c_newvalue2', title: __('Hjdevice.sxmda_c_newvalue2'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_c_newvalue3', title: __('Hjdevice.sxmda_c_newvalue3'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_c_newvalue4', title: __('Hjdevice.sxmda_c_newvalue4'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_d_newvalue1', title: __('Hjdevice.sxmda_d_newvalue1'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_d_newvalue2', title: __('Hjdevice.sxmda_d_newvalue2'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_d_newvalue3', title: __('Hjdevice.sxmda_d_newvalue3'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_d_newvalue4', title: __('Hjdevice.sxmda_d_newvalue4'), operate:'BETWEEN'},
                        //{field: 'hjdevice.sxmda_code', title: __('Hjdevice.sxmda_code')},
                        //{field: 'hjdevice.zzycgj_code', title: __('Hjdevice.zzycgj_code')},
                        //{field: 'hjdevice.isonline', title: __('Hjdevice.isonline')},
                        //{field: 'hjdevice.create_time', title: __('Hjdevice.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'hjdevice.update_time', title: __('Hjdevice.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_wu_hjdevicelog', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_wu_hjdevicelog')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wu_hjdevicelog')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        quxt: function () {

            $(document).on("click", "#dosearch", function () {
                var url = 'quxt?';
                var formdata = $("#a_form").serialize();
                self.window.open(url + formdata);

                //重刷本页面
                window.location.reload();
                return false;
            });

            console.log(WSdata.datetimerange);//无需传入，重刷页面即可

            Controller.xiantu();
            Controller.api.bindevent();
        },
        xiantu: function () {
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            data = [["2000-06-05",116],["2000-06-06",129],["2000-06-07",135],["2000-06-08",86],["2000-06-09",73],["2000-06-10",85],["2000-06-11",73],["2000-06-12",68],["2000-06-13",92],["2000-06-14",130],["2000-06-15",245],["2000-06-16",139],["2000-06-17",115],["2000-06-18",111],["2000-06-19",309],["2000-06-20",206],["2000-06-21",137],["2000-06-22",128],["2000-06-23",85],["2000-06-24",94],["2000-06-25",71],["2000-06-26",106],["2000-06-27",84],["2000-06-28",93],["2000-06-29",85],["2000-06-30",73],["2000-07-01",83],["2000-07-02",125],["2000-07-03",107],["2000-07-04",82],["2000-07-05",44],["2000-07-06",72],["2000-07-07",106],["2000-07-08",107],["2000-07-09",66],["2000-07-10",91],["2000-07-11",92],["2000-07-12",113],["2000-07-13",107],["2000-07-14",131],["2000-07-15",111],["2000-07-16",64],["2000-07-17",69],["2000-07-18",88],["2000-07-19",77],["2000-07-20",83],["2000-07-21",111],["2000-07-22",57],["2000-07-23",55],["2000-07-24",60]];

            var dateList = data.map(function (item) {
                return item[0];
            });
            var valueList = data.map(function (item) {
                return item[1];
            });

            var option = {

                // Make gradient line here
                visualMap: [{
                    show: false,
                    type: 'continuous',
                    seriesIndex: 0,
                    min: 0,
                    max: 400
                }, {
                    show: false,
                    type: 'continuous',
                    seriesIndex: 1,
                    dimension: 0,
                    min: 0,
                    max: dateList.length - 1
                }],


                title: [{
                    left: 'center',
                    text: '温度'
                }, {
                    top: '55%',
                    left: 'center',
                    text: '湿度'
                }],
                tooltip: {
                    trigger: 'axis'
                },
                xAxis: [{
                    data: WSdata.wenXdata
                }, {
                    data: WSdata.shiXdata,
                    gridIndex: 1
                }],
                yAxis: [{
                    splitLine: {show: false}
                }, {
                    splitLine: {show: false},
                    gridIndex: 1
                }],
                grid: [{
                    bottom: '60%'
                }, {
                    top: '60%'
                }],
                series: [{
                    type: 'line',
                    showSymbol: false,
                    data: WSdata.wendata
                }, {
                    type: 'line',
                    showSymbol: false,
                    data: WSdata.shidata,
                    xAxisIndex: 1,
                    yAxisIndex: 1
                }]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);

        },
        zext: function () {
            $(document).on("click", "#dosearch", function () {
                var url = 'zext?';
                var formdata = $("#a_form").serialize();
                self.window.open(url + formdata);

                window.location.reload();
                return false;
            });

            Controller.zhetu();
            Controller.api.bindevent();

        },
        zhetu: function () {
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            var option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['L1', 'L2', 'L3', 'L4', 'L5', 'L6']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                //toolbox: {
                //    feature: {
                //        saveAsImage: {}
                //    }
                //},
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: WSdata.mdXlist
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: 'L1',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.mdlist1
                    },
                    {
                        name: 'L2',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.mdlist2
                    },
                    {
                        name: 'L3',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.mdlist3
                    },
                    {
                        name: 'L4',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ff0000',
                                lineStyle:{
                                    color:'#ff0000'
                                }
                            }
                        },
                        data: WSdata.mdlist4
                    },
                    {
                        name: 'L5',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ff9900',
                                lineStyle:{
                                    color:'#ff9900'
                                }
                            }
                        },
                        data: WSdata.mdlist5
                    },
                    {
                        name: 'L6',
                        type: 'line',
                        stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ffee00',
                                lineStyle:{
                                    color:'#ffee00'
                                }
                            }
                        },
                        data: WSdata.mdlist6
                    },
                ]
            };
            myChart.setOption(option);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});